﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF_MVVM_PathSearchUtil.Models;

namespace WPF_MVVM_PathSearchUtil.util
{
    public class ListConvertFromDB
    {
        /// <summary>
        /// 将从服务器端获得的JArray数组转换为 fileList
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="fileList"></param>
        public static void JArrayToList(ref ObservableCollection<FileInfoModel> fileListRead, JArray fileList)
        {
            foreach (JObject fileInfo in fileList)
            {
                FileInfoModel fileInfoModel = new FileInfoModel();
                fileInfoModel.FileName = fileInfo["fileName"].ToString();
                fileInfoModel.FilePath = fileInfo["filePath"].ToString();
                fileInfoModel.FileType = fileInfo["fileType"].ToString();
                fileInfoModel.FileSize = double.Parse(fileInfo["fileSize"].ToString());
                fileInfoModel.FileDate = DateTime.Parse(fileInfo["fileDate"].ToString());
                fileListRead.Add(fileInfoModel);
            }
        }

        /// <summary>
        /// 把本地db获取的list赋给filelist
        /// </summary>
        /// <param name="fileList"></param>
        /// <param name="localList"></param>
        public static void LocalToList(ref ObservableCollection<FileInfoModel> fileList, Object localList)
        {
            ObservableCollection<FileInfoModel> list = (ObservableCollection<FileInfoModel>)localList;
            foreach (FileInfoModel fileInfo in list)
            {
                fileList.Add(fileInfo);
            }
        }
    }
}
